class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        int hash1[26] = { 0 }, hash2[26] = { 0 };
        for (auto& e : p)
        {
            hash2[e - 'a']++;
        }
        int left = 0, right = 0, n1 = s.size(), n2 = p.size(), count = 0;
 
        vector<int> ret;
        if(n2 > n1)
            return ret;
 
        while (right < n1)
        {
            char in = s[right];
            if(++hash1[in - 'a'] <= hash2[in - 'a'])
            {
                ++count;  // 进窗口和维护count
            }
 
            if(right - left + 1 > n2) // 判断
            {
                char out = s[left++];
                if(hash1[out - 'a']-- <= hash2[out - 'a'])
                {
                    --count; // 出窗口和维护count
                }
            }
 
            if (count == n2) // 更新结果
            {
                ret.push_back(left);
            }
            ++right;
        }
        return ret;
    }
};